import store from '@/store'
import nedb from '@/db/nedb'
import { Message } from 'element-ui'
// ============================以下是通知=============================
// 朋友圈删除通知 ok
function circleDelNoticeHandle(msg) {
    let message = JSON.parse(msg.message)
    // 从本地删除数据
    store.dispatch('nedb/RemoveCircles', message)
}

// 新朋友圈通知 ok
function circleNewPublishNoticeHandle(msg) {
    let message = JSON.parse(msg.message)
    let newCircles = {
        WeChatId: message.WeChatId,
        Circles: [message.Circle]
    }
    store.dispatch('nedb/UpdateOrAddCircles', newCircles)
}

// 朋友圈点赞通知 取消点赞不会通知 ok
function circleLikeNoticeHandle(msg) {
    let message = JSON.parse(msg.message)
    let like = {
        FriendId: message.FriendId,
        PublishTime: message.PublishTime
    }
    // 更新本地数据库
    nedb.circles.findOne(
        { WeChatId: message.WeChatId, FriendId: message.WeChatId },
        (err, cdoc) => {
            if (err || !cdoc) return
            for (const circle of cdoc.Circles) {
                if (circle.CircleId === message.CircleId) {
                    if (circle.Likes) {
                        circle.Likes.push(like)
                    } else {
                        circle.Likes = [like]
                    }
                    let newCircles = {
                        WeChatId: message.WeChatId,
                        Circles: [circle]
                    }
                    store.dispatch('nedb/UpdateOrAddCircles', newCircles)
                    break
                }
            }
        })
}

// 评论通知 ok
function circleCommentNoticeHandle(msg) {
    let message = JSON.parse(msg.message)
    // 更新本地数据库
    nedb.circles.findOne(
        { WeChatId: message.WeChatId, FriendId: message.WeChatId },
        (err, cdoc) => {
            if (err || !cdoc) return
            for (const circle of cdoc.Circles) {
                if (circle.CircleId === message.CircleId) {
                    if (circle.Comments) {
                        circle.Comments.push(message.Comment)
                    } else {
                        circle.Comments = [message.Comment]
                    }
                    let newCircles = {
                        WeChatId: message.WeChatId,
                        Circles: [circle]
                    }
                    store.dispatch('nedb/UpdateOrAddCircles', newCircles)
                    break
                }
            }
        })
}

// ============================以下是任务结果=============================

// 返回指定微信的朋友圈信息 ok
function circlePushNoticeHandle(msg) {
    let message = JSON.parse(msg.message)
    // 如果有朋友圈数据
    if (message.Circles && message.Circles.length > 0) {
        // 更新或插入数据库
        store.dispatch('nedb/UpdateOrAddCircles', message)
    } else {
        Message({
            type: 'warning',
            message: '获取不到朋友圈',
            duration: 1000
        })
    }
}

// 获取朋友圈图片详情任务结果 ok
function circleDetailNoticeHandle(msg) {
    let message = JSON.parse(msg.message)
    store.dispatch('nedb/UpdateCirclesImgs', message)
    Message({
        type: 'success',
        message: '获取朋友圈图片成功！',
        duration: 1000,
    })
}

// 删除朋友圈评论 ok
function circleCommentDeleteTaskResultNoticeHandle(msg) {
    let message = JSON.parse(msg.message)
    // 更新任务表
    store.dispatch('nedb/UpdateCircleTask', message)

    // 更新本地数据库
    if (message.Success) {
        Message({
            type: 'success',
            message: '删除评论成功！',
            duration: 1000,
        })
        nedb.circleTask.findOne({ TaskId: message.TaskId }, (err, circleTaskDoc) => {
            if (circleTaskDoc) {
                let friendId = circleTaskDoc.FriendId || message.WeChatId
                nedb.circles.findOne(
                    { WeChatId: message.WeChatId, FriendId: friendId },
                    (err, cdoc) => {
                        if (err || !cdoc) return
                        for (const circle of cdoc.Circles) {
                            if (circle.CircleId === message.CircleId) {
                                let comments = circle.Comments
                                if (comments) {
                                    let i = 0
                                    for (const comment of comments) {
                                        if (comment.CommentId === message.CommentId) {
                                            comments.splice(i, 1)
                                            circle.Comments = comments
                                            let newCircles = {
                                                WeChatId: message.WeChatId,
                                                Circles: [circle]
                                            }
                                            store.dispatch('nedb/UpdateOrAddCircles', newCircles)
                                            break
                                        }
                                        i++
                                    }
                                }
                                break
                            }
                        }
                    })
            }
        })
    } else {
        Message({
            type: 'warning',
            message: '删除评论失败！',
            duration: 2000,
        })
    }

}

// 朋友圈评论结果 ok
function circleCommentReplyTaskResultNoticeHandle(msg) {
    let message = JSON.parse(msg.message)
    // 更新任务表
    store.dispatch('nedb/UpdateCircleTask', message)
    // 更新本地数据库
    if (message.Success) {
        Message({
            type: 'success',
            message: '评论成功！',
            duration: 1000,
        })
        // 从任务表查询评论的内容
        nedb.circleTask.findOne({ TaskId: message.TaskId }, (err, circleTaskDoc) => {
            if (circleTaskDoc) {
                let friendId = circleTaskDoc.FriendId || message.WeChatId
                // 要插入的comment
                let comment = {
                    CommentId: message.CommentId,
                    Content: circleTaskDoc.Content,
                    FromWeChatId: message.WeChatId,
                    PublishTime: message.PusblishTime,
                }
                // 如果是回复别人的评论
                if (circleTaskDoc.ToWeChatId) {
                    comment.ToWeChatId = circleTaskDoc.ToWeChatId
                }
                // 如果是回复别人的评论
                if (circleTaskDoc.FromWeChatId) {
                    comment.FromWeChatId = circleTaskDoc.FromWeChatId
                }
                // 更新本地数据
                nedb.circles.findOne(
                    { WeChatId: message.WeChatId, FriendId: friendId },
                    (err, cdoc) => {
                        if (err || !cdoc) return
                        for (const circle of cdoc.Circles) {
                            if (circle.CircleId === message.CircleId) {
                                if (circle.Comments) {
                                    circle.Comments.push(comment)
                                } else {
                                    circle.Comments = [comment]
                                }
                                let newCircles = {
                                    WeChatId: message.WeChatId,
                                    Circles: [circle]
                                }
                                store.dispatch('nedb/UpdateOrAddCircles', newCircles)
                                break
                            }
                        }
                    })
            }
        })
    } else {
        Message({
            type: 'warning',
            message: '评论失败！',
            duration: 2000,
        })
    }
}

/**
 * 发送朋友圈结果通知 ok
 */
function postSNSNewsTaskResultNoticeHandle(msg) {
    let message = JSON.parse(msg.message)
    // 更新本地任务日志
    store.dispatch('nedb/UpdateCircleTask', message)
    if (message.Success) {
        Message({
            type: 'success',
            message: '朋友圈发送成功！',
            duration: 1000,
        })
    } else {
        Message({
            type: 'warning',
            message: '朋友圈发送失败！',
            duration: 2000,
        })
    }
}





export default {
    circleDelNoticeHandle,
    circlePushNoticeHandle,
    postSNSNewsTaskResultNoticeHandle,
    circleDetailNoticeHandle,
    circleLikeNoticeHandle,
    circleCommentReplyTaskResultNoticeHandle,
    circleCommentDeleteTaskResultNoticeHandle,
    circleCommentNoticeHandle,
    circleNewPublishNoticeHandle
}
